package com.mdxq.config;


import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @author WanJl
 * @version 1.0
 * @title RedisConfig
 * @description redis配置类
 * @create 2025/1/11
 */
@Configuration
public class RedisConfig extends CachingConfigurerSupport {

    @Bean
    public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<Object, Object> redisTemplate=new RedisTemplate<>();
        // 设置字符串序列化器
        //设置字符串类型key的序列化器
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        // 设置字符串类型value的序列化器
        redisTemplate.setValueSerializer(new StringRedisSerializer());

        // 设置hash类型key的序列化器
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());


        //把连接工厂对象传递给redisTemplate对象，让redisTemplate能够使用这个连接工厂来创建和管理redis连接
        //这样，就可以把我们修改后的序列化器在redis中有效
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        return redisTemplate;
    }

}
